package com.tuanzi.loan.web.bean.application;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.application.LoanApplicationExport;
import com.tuanzi.loan.business.entity.application.LoanApplicationExportGroup;
import com.tuanzi.loan.business.entity.application.LoanApplicationExportGroupPage;
import com.tuanzi.loan.business.entity.common.CommProductChannel;
import com.tuanzi.loan.business.entity.common.CommProductId;
import com.tuanzi.loan.business.service.application.LoanApplicationExportGroupPageService;
import com.tuanzi.loan.business.service.application.LoanApplicationExportGroupService;
import com.tuanzi.loan.business.service.application.LoanApplicationExportService;
import com.tuanzi.loan.business.service.common.CommProductIdService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.application.LoanApplicationExportGroupRequest;
import com.tuanzi.loan.vo.request.application.LoanApplicationExportRequest;
import com.tuanzi.loan.vo.response.application.LoanApplicationExportResponse;
import com.tuanzi.loan.vo.response.family.IcpWebCommentSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.application.LoanApplicationExportGroupLazyDataModel;
import com.tuanzi.loan.web.lazy.application.LoanApplicationExportLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.EXPORT_TYPE;

/**
 * @Auther: hequnayu
 * @Date: 2019/7/31 15:02
 * @Description:
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanApplicationExportListBean extends JSFListingBean<LoanApplicationExportRequest,LoanApplicationExportResponse> {
    private static final long serialVersionUID = -88985886600621814L;

    @Autowired
    private LoanApplicationExportService exportService;
    @Autowired
    private LoanApplicationExportGroupService groupService;
    @Autowired
    private LoanApplicationExportGroupPageService pageService;
    @Autowired
    private CommProductIdService commProductIdService;

    private List<CommProductId> commProductIds;
    private List<CommProductChannel> commProductChannels;
    private List<SelectItem> exportGroup;
    @PostConstruct
    private void init() {
        commProductIds = commProductIdService.findAll().stream().filter(x -> x.getRealPrdid() != null).collect(Collectors.toList());
        commProductChannels = super.getCommProductChannels();
        initExportGroup();
        initLazyDataModel();
    }

    public void onReset() {
        initLazyDataModel();
    }

    public String onAdd() {
        return redirect("/views/application/export/info.xhtml");
    }

    public String onEdit(String id) {
        return redirect("/views/application/export/info.xhtml?id=" + id);
    }

    public void onDelete(Long id){
        exportService.delete(exportService.findOne(id));
    }
    public void onBatchDelete(){
        for (LoanApplicationExportResponse response : selectedRecords){
            onDelete((Long) response.getId());
        }
    }

    public void online(Long id){
        LoanApplicationExport export = exportService.findOne(id);
        exportService.online(export);
    }
    public void offline(Long id){
        LoanApplicationExport export = exportService.findOne(id);
        exportService.offline(export);
    }

    private void initLazyDataModel() {
        $ = new LoanApplicationExportRequest();
        lazyDataModel = new LoanApplicationExportLazyDataModel($);
    }

    public void initExportGroup(){
        exportGroup = Lists.newArrayList();
        List<LoanApplicationExportGroupPage> pages = pageService.findAll();
        pages.stream().forEach((x) -> {
            SelectItemGroup group = new SelectItemGroup(x.getName());
            List<LoanApplicationExportGroup> pageGroup = groupService.findByPageId(x.getId());
            group.setSelectItems(pageGroup.stream().map(g -> new SelectItem(g.getId(),g.getName())).toArray(SelectItem[]::new));
            exportGroup.add(group);
        });
    }
    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(EXPORT_TYPE);
    }
}
